<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <!-- <HighFormQuery :options="searchOptions" @submit="handleQuery" /> -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="资源名称" prop="sourceName">
        <el-input
          v-model="queryParams.sourceName"
          placeholder="请输入资源名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资源编号" prop="sourceCode">
        <el-input
          v-model="queryParams.sourceCode"
          placeholder="请输入资源编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资源类型" prop="sourceType">
        <el-select
          v-model="queryParams.sourceType"
          placeholder="请选择资源类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资源来源" prop="sourceFrom">
        <!-- <el-select
          v-model="queryParams.sourceFrom"
          placeholder="请选择资源来源"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE_FROM)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select> -->
        <el-input
          v-model="queryParams.sourceFrom"
          placeholder="请输入资源来源"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <span v-show="isShow">
        <el-form-item label="来源信息" prop="sourceDesc">
          <el-select
            v-model="queryParams.sourceDesc"
            placeholder="请选择资源来源"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE_DESC)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="资源描述" prop="catalogDesc">
          <el-input
            v-model="queryParams.catalogDesc"
            placeholder="请输入资源描述"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="主题分类" prop="catalogClassify">
          <el-select
            v-model="queryParams.catalogClassify"
            placeholder="请选择主题分类"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.JOURNAL_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="资源标签" prop="catalogTags">
          <el-tree-select
            v-model="queryParams.catalogTags"
            class="!w-240px"
            :data="tagList"
            :props="{
              label: 'tagName',
              value: 'id',
              children: 'children',
              disabled: (node) => node.children && node.children.length > 0,
            }"
            :render-after-expand="false"
            multiple
            check-on-click-node
            collapse-tags
            filterable
            clearable
          />
        </el-form-item>
        <el-form-item label="存放位置" prop="catalogStore">
          <el-input
            v-model="queryParams.catalogStore"
            placeholder="请输入存放位置"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
      </span>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="text" @click="isShow = !isShow">{{ isShow ? '收缩' : '展开'}}</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="#" align="center" type="index" width="50" fixed="left" />
      <el-table-column label="封面" align="center" prop="mainImage">
        <template #default="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.mainImage"
            :preview-src-list="[scope.row.mainImage]"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="资源名称" align="center" prop="sourceName" />
      <el-table-column label="资源编号" align="center" prop="sourceCode" />
      <el-table-column label="资源类型" align="center" prop="sourceType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SOURCE_TYPE" :value="scope.row.sourceType" />
        </template>
      </el-table-column>
      <el-table-column label="来源信息" align="center" prop="sourceDesc">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SOURCE_DESC" :value="scope.row.sourceDesc" />
        </template>
      </el-table-column>
      <el-table-column label="资源来源" align="center" prop="sourceFrom">
        <template #default="scope">
          <!-- <dict-tag :type="DICT_TYPE.SOURCE_FROM" :value="scope.row.sourceFrom" /> -->
          {{ scope.row.sourceFrom }}
        </template>
      </el-table-column>
      <el-table-column label="文件类型" align="center" prop="fileType" />
      <el-table-column label="主题分类" align="center" prop="catalogClassify">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.JOURNAL_TYPE" :value="scope.row.catalogClassify" />
        </template>
      </el-table-column>
      <el-table-column label="资源标签" align="center" prop="catalogTags">
        <template #default="scope">
          <DictCataTag :value="scope.row.catalogTags" />
        </template>
      </el-table-column>
      <el-table-column label="存放位置" align="center" prop="catalogStore" />
      <el-table-column label="资源描述" align="center" prop="catalogDesc">
        <template #default="scope">
          <span v-html="scope.row.catalogDesc"></span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="center" min-width="60px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row)"
            v-hasPermi="['dr:source-cata:query']"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 详情 -->
  <SourceSearchDetail v-model:visible="detailVisible" :data="currentRow" />
 
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { handleTree } from '@/utils/tree'
import { SourceApi, SourceVO } from '@/api/dr/source'
import { SourceCataVO } from '@/api/dr/sourcecata'
import { SourceTagApi, SourceTagVO } from '@/api/dr/sourcetag'

/** 资源信息 列表 */
defineOptions({ name: 'SourceCata' })

const isShow = ref<Boolean>(false)
const tagList = ref<SourceTagVO[]>([])
const loading = ref(true) // 列表的加载中
const list = ref<any[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive<SourceVO | SourceCataVO | any>({
  pageNo: 1,
  pageSize: 10,
  sourceId: undefined,
  sourceName: undefined,
  sourceCode: undefined,
  sourceType: undefined,
  sourceTag: undefined,
  sourceDesc: undefined,
  fileType: undefined,
  fileSize: undefined,
  file: undefined,
  sourceFrom: undefined,
  status: undefined,
  createTime: undefined,
  catalogDesc: undefined,
  catalogClassify: undefined,
  catalogTags: undefined,
  catalogStore: undefined,
  webUrl: undefined,
  updateTime: undefined,
})
const searchOptions = ref([
  {
    label: '资源名称',
    value: 'sourceName',
  },
  {
    label: '资源编号',
    value:'sourceCode',
  },
  {
    label: '资源类型',
    value:'sourceType',
  },
  {
    label: '来源信息',
    value:'sourceDesc',
  },
  {
    label: '文件类型',
    value:'fileType',
  },
  {
    label: '作者名称',
    value:'authorName',
  },
  {
    label: '资源标签',
    value:'catalogTags',
  },
  {
    label: '主题分类',
    value:'catalogClassify',
  },
  {
    label: '资源描述',
    value:'catalogDesc',
  },
  {
    label: '存放位置',
    value:'catalogStore',
  },
  {
    label: '网络连接',
    value:'webUrl',
  },
  {
    label: '入库时间',
    value:'createTime',
  },
  {
    label: '更新时间',
    value:'updateTime',
  },

])
const detailVisible = ref<boolean>(false)
const currentRow = ref<SourceVO>()
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const queryData = {
      ...queryParams,
      catalogTags: queryParams.catalogTags?.join(','),
    }
    const data = await SourceApi.getSourceSearchPage(queryData)
    const dataList = data.list || []
    list.value = dataList
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 查询tag列表 */
const getTagList = async () => {
  try {
    const data = await SourceTagApi.getSourceTagPage({
      pageNo: 1,
      pageSize: 100,
    })
    data.list.forEach(v => {
      v.id = v.id + ''
    })
    tagList.value = handleTree(data.list, 'id', 'parentId', 'children')
  } finally {
  }
}

/** 搜索按钮操作 */
const handleQuery = (params = {}) => {
  console.log('handleQuery params', params)
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

// 详情弹窗
const openDetail = (row: any) => {
  currentRow.value = { ...row }
  detailVisible.value = true
}

/** 初始化 **/
onMounted(() => {
  getTagList()
  getList()
})
</script>